﻿using ERPPublic;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProcurementManagement.Applcation.Command.QualityCommand;
using ProcurementManagement.Domain.Dtos.Quality;
using ProcurementManagement.Dtos;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
using System.Security.Claims;
using System.IdentityModel.Tokens.Jwt;
using Microsoft.AspNetCore.Authorization;

namespace ProcurementManagement.Controllers
{
    /// <summary>
    /// 产品质检审核
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class ProAugitController : ControllerBase
    {


        IMediator _mediator;


        public ProAugitController(IMediator mediator)
        {
            _mediator = mediator;
        }


        /// <summary>
        /// 产品质检添加
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddProAugit(AddProductQualityDTO dto)
        {
            ApiResult<int> result = new ApiResult<int>();

            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                dto.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                dto.AddName = name;
                dto.AddTime = DateTime.Now;
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                dto.User_Id = int.Parse(userId);
                result.Data = await _mediator.Send(new CreateProQCommand { AddDTO = dto });
            }
            catch (Exception ex)
            {
                result.Data = 500;
                result.Message = ex.Message;

                throw;
            }
            return result;
        }


        /// <summary>
        /// 产品质检列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<SelectProAuditDTO>>> ShowProAugit([FromQuery] SelectProQAugitCommand command)
        {
            ApiResult<PageDTO<SelectProAuditDTO>> result = new ApiResult<PageDTO<SelectProAuditDTO>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }

    }
}
